package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.productionPlan.dao.ProQualityTaskDao;
import com.wing.productionPlan.dao.QualityTestingRecordDetailDao;
import com.wing.productionPlan.model.entity.ProQualityTask;
import com.wing.productionPlan.model.entity.QualityTestingRecordDetail;
import com.wing.productionPlan.model.entity.SimpleWorkOrder;
import com.wing.productionPlan.model.request.ProQualityTaskAddForm;
import com.wing.productionPlan.model.request.QualityTestingRecordDetailForm;
import com.wing.productionPlan.model.response.ProQualityTaskVo;
import com.wing.productionPlan.model.response.ProductionPlanVO;
import com.wing.productionPlan.model.response.QualityTestingRecordDetailVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首件检测任务-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProQualityTaskServiceImpl extends ServiceImpl<ProQualityTaskDao, ProQualityTask> implements ProQualityTaskService {

    /**
     * 查询首件检测任务
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ProQualityTaskVo> pageList4app(Page<ProQualityTaskVo> pg, Map map) {
        List<ProQualityTaskVo> tasks = this.baseMapper.pageList4app(pg,map);
        pg.setRecords(tasks);
        return pg;
    }

    /**
     * 添加质检任务
     * @param proQualityTaskAddForm
     * @return
     */
    @Override
    public int add(ProQualityTaskAddForm proQualityTaskAddForm) {
        String code = UniqueIdGeneratorUtil.generateUniqueId("ZJ", "yyyyMMdd", 1000, 9999);
        ProQualityTask proQualityTask =new ProQualityTask();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProQualityTask.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(proQualityTaskAddForm, proQualityTask, copyOptions);
        proQualityTask.setCreateTime(LocalDateTime.now());
        proQualityTask.setCreator(JwtUtils.getLoginUserId());
        proQualityTask.setSjPerson(JwtUtils.getLoginUserId());
        proQualityTask.setSjTime(LocalDateTime.now());
        proQualityTask.setSjWorkstation(proQualityTaskAddForm.getDeviceName());
        proQualityTask.setStatus(String.valueOf(MaintenanceEnum.QualityStatus.DRL.getCode()));
        proQualityTask.setCode(code);
        return baseMapper.insert(proQualityTask);
    }

    /**
     * 关闭任务
     * @param id
     * @return
     */
    @Override
    public boolean closeTask(Long id) {
        ProQualityTask proQualityTask = this.baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(proQualityTask)){
            proQualityTask.setStatus(String.valueOf(MaintenanceEnum.QualityStatus.GB.getCode()));
            proQualityTask.setUpdateTime(LocalDateTime.now());
            proQualityTask.setModifier(JwtUtils.getLoginUserId());
            proQualityTask.setModifierName(JwtUtils.getLoginUsername());
            int i = this.baseMapper.updateById(proQualityTask);
            return i > 0;
        }
        return false;
    }
}
